// 基础类型定义

export type u8 = number;
export type u16 = number;
export type u32 = number;
export type u64 = bigint;
export type i8 = number;
export type i16 = number;
export type i32 = number;
export type i64 = bigint;

export const PAGE_SIZE = 4096;  // 页大小

export enum MachineMode {
  REAL = 0,
  LEGACY = 1,
  LONG = 2
}

export enum Registers {
  RAX = 0,
  RCX = 1,
  RDX = 2,
  RBX = 3,
  RSP = 4,
  RBP = 5,
  RSI = 6,
  RDI = 7,
  R8 = 8,
  R9 = 9,
  R10 = 10,
  R11 = 11,
  R12 = 12,
  R13 = 13,
  R14 = 14,
  R15 = 15
}

export enum SegmentRegisters {
  ES = 0,
  CS = 1,
  SS = 2,
  DS = 3,
  FS = 4,
  GS = 5
}

export const REGISTER_COUNT = 16;
export const SEGMENT_REGISTER_COUNT = 6;
export const XMM_REGISTER_COUNT = 16;
export const XMM_REGISTER_SIZE = 16;  // 128 bits

export enum MemoryProtection {
  NONE = 0,
  READ = 1 << 0,
  WRITE = 1 << 1,
  EXECUTE = 1 << 2,
  READ_WRITE = READ | WRITE,
  READ_EXECUTE = READ | EXECUTE,
  READ_WRITE_EXECUTE = READ | WRITE | EXECUTE
}

export enum ErrorCodes {
  EXIT_FAILURE_EXEC_FAILED = 127,
  EXIT_FAILURE_WITH_SIGNAL = 128
}

export function toU64(value: number): u64 {
  return BigInt.asUintN(64, BigInt(value));
}

export function toU32(value: number): u32 {
  return value >>> 0;
}

export function toI64(value: number): i64 {
  return BigInt(value);
}